//app.js
App({
  onLaunch: function (res) {
    //获取桌牌号，并存储在本地缓存中
    var desk = res.query;
    wx.setStorageSync('desk', desk);
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录 获取js_code
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        this.globalData.jscode = res.code
        wx.getUserInfo({
          success: res => {
            // 可以将 res 发送给后台解码出 unionId
            this.globalData.userInfo = res.userInfo
            console.log(res.userInfo)
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            if (this.userInfoReadyCallback) {
              this.userInfoReadyCallback(res)
            }
          }
        })
      }
    })
  },
  // 获取openid
  getUser(callBack){
        var that = this
        wx.request({
          url: this.globalData.yuming + 'index.php/Api/Login/getOpenid',
          data: {
            js_code: this.globalData.jscode
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          method: "POST",
          success: function (res) {
            //  赋值全局的openid
            that.globalData.openid = res.data.openid
            callBack(res.data.openid)
          }
        })
  },
  // 获取购物车总价
  getTotal(data,callBack){
      wx.request({
        url: this.globalData.yuming + 'index.php/Api/Cart/cartTotal',
        data: {
          m_id: data
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        success: function (res) {
          callBack(res)
        }
      })
  },
  globalData: {
    userInfo: null,
    yuming:"http://dc.zxty.me/",
    openid:"",
    jscode:"",
    m_id:"",
    total:"",
    // 确认订单时提交的商品列表
    goodList:[]
  }
})